package com.mtao.controller.admin;


import com.mtao.*;
import com.mtao.dto.IdDto;
import com.mtao.utils.StringUtil;
import com.mtao.entity.SysRoleMenu;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private SysRoleService roleService;

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Resource
    private SysRoleUserService sysRoleUserService;

    /**
     * 获取角色列表
     * @return
     */
    @PostMapping("/list")
    public com.mtao.entity.Result getRoleList(){
        List<com.mtao.entity.SysRole> roleList = roleService.findRoleList();
        for (com.mtao.entity.SysRole sysrole:roleList) {
            List<com.mtao.entity.SysRoleMenu> roleMenu = sysRoleMenuService.getRoleMenu(sysrole.getId());
            ArrayList<Integer> list = new ArrayList<>();
            for (com.mtao.entity.SysRoleMenu sysRoleMenu:roleMenu) {
                if(sysrole.getId().equals(sysRoleMenu.getRoleId())){
                    list.add(sysRoleMenu.getMenuId());
                }
            }
            sysrole.setMenuIds(list);
        }
        return com.mtao.entity.Result.success(roleList);
    }

    /**
     * 新增角色
     * @param sysRole
     * @return
     */
    @PostMapping("/add")
    public com.mtao.entity.Result addRole(@RequestBody @Validated(com.mtao.entity.SysRole.Add.class) com.mtao.entity.SysRole sysRole){
        if(sysRole.getId()==null){
            boolean flag=roleService.queryRoleName(sysRole.getRoleName());
            if(flag){return com.mtao.entity.Result.error("角色已存在");}
            roleService.addRole(sysRole);
            roleService.addRole(sysRole);
        }else {
            roleService.updateRole(sysRole);
        }
        LocalDateTime localDateTime = LocalDateTime.now();
        ArrayList menuIds = sysRole.getMenuIds();
//        Map<String, Object> map = ThreadLocalUtil.get();
//        Integer userId = (Integer) map.get("userId");
        //获取角色id
        Integer rolesId = sysRole.getId();
        List<com.mtao.entity.SysRoleMenu> sysRoleMenus = new ArrayList<>();
        if (menuIds.size()>0){
            sysRoleMenuService.batchDeleteRoleMenu(rolesId);
            for (Object id : menuIds) {
                com.mtao.entity.SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(rolesId);
                sysRoleMenu.setMenuId((Integer) id);
                sysRoleMenu.setCreateTime(localDateTime);
                sysRoleMenus.add(sysRoleMenu);
            }
            sysRoleMenuService.batchAddRoleMenu(sysRoleMenus);
        }
        return com.mtao.entity.Result.success();
    }
    /**
     * 删除角色
     * @param idDto
     * @return
     */
    @PostMapping("/delete")
    public com.mtao.entity.Result deleteRole(@RequestBody IdDto idDto){
        int[] ints = StringUtil.StringToNumberArray(idDto.getId());
        for (int anInt : ints) {
            roleService.deleteRole(anInt);
        }
        return com.mtao.entity.Result.success();
    }

}
